<?

$check_me_authed = "NO_OK";

if(($jid = $_POST["radioview"]) || ($jid = $_POST["job_id_posted"]) ){

	$COracle = new oracleDb;
  $COracle->init($_SESSION["ORCL"],
                 $_SESSION["OPASS"],
                 $_SESSION["SID"],
                 _NO_COMMENT);

	if ($COracle->connect()) {

  	$srcDbHdr			      = "PCH_HDR";
    $srcDbHdrLog	      = "LOG_PCH_HDR";
    $srcDbDtl           = "PCH_DTL";
		$srcDbSubDtl        = "PCH_SUB_DTL";

		$_SESSION["jid"]  = $_POST["radioview"];
		$jobId            = &$_SESSION["jid"];

		/**
		* Fetch global header
		* And check the status if it is valid
		* where STATUS = _NOT_ACTIVE
		*
		*/
		$CFetchers->fetchLstDataHeader($COracle,
                                   $jobId,
                                   $srcDbHdr,
                                   $nHeader,
                                   $lstHeader);

		if($lstHeader["STATUS"][0] == _NOT_ACTIVE ||
		   $lstHeader["STATUS"][0] == _CANCELED){

  		/**
  		* Init the header vars
  		*
  		*/
  		$_POST["trDateEnt"]       = date("d-m-Y",
  		                                 strtotime($lstHeader["TR_DATE"][0]));
  		$_POST["dueDateEnt"]      = date("d-m-Y",
  		                                 strtotime($lstHeader["DUE_DATE"][0]));

      $_POST["suppCstNameEnt"]  = $lstHeader["SUPPLIER_NAME"][0];
      $_POST["suppCstCodeEnt"]  = $lstHeader["SUPPLIER_CODE"][0];
      $_POST["notaSrcEnt"]      = $lstHeader["NOTA_SRC"][0];

  		$_POST["currencyEnt"]     = $lstHeader["CURRENCY"][0];
  		$_POST["rateEnt"]         = $lstHeader["RATE"][0];
  		$_POST["crTermEnt"]       = $lstHeader["CREDIT_TERM"][0];

  		$_POST["totAmount"]       = $lstHeader["AMOUNT"][0];
  		$_POST["totTaxPctg"]      = $lstHeader["TAX_PCTG"][0];
  		$_POST["totTax"]          = $lstHeader["TAX"][0];
  		$_POST["gDiscPctg"]       = $lstHeader["DISC_PCTG"][0];
  		$_POST["totGDisc"]        = $lstHeader["DISC"][0];

  		$_POST["totExpense"]      = $lstHeader["EXPENSE"][0];
  		$_POST["totPrepaid"]      = $lstHeader["PREPAID"][0];
  		$_POST["totPayable"]      = $lstHeader["PAYABLE"][0];
  		$_POST["memoEnt"]         = $lstHeader["MEMO"][0];

  		/**
  		* Init the detail
  		*
  		*/
  		$CLFetchers->fetchLstDataDetail($COracle,
                                      $jobId,
                                      $srcDbDtl,
                                      $nDetail,
                                      $lstDetail);

		  $_POST["totHrzTotEnt"] = $lstDetail["TOT_HRZ_TOT"][0];
		  $_POST["totDiscEnt"]   = $lstDetail["TOT_DISC"][0];
		  $_POST["totNetEnt"]    = $lstDetail["TOT_NET"][0];

		  $CLFetchers->fetchLstDataDetail($COracle,
                                      $jobId,
                                      $srcDbSubDtl,
                                      $nSubDetail,
                                      $lstSubDetail);

		  for($iSub = 0; $iSub < $nSubDetail; $iSub++){

  		  /**
  		  * Init the normal vars
  		  *
  		  */
		    $_POST["whsEnt"][$iSub]          = $lstSubDetail["WAREHOUSE"][$iSub];
  		  $_POST["modelEnt"][$iSub]        = $lstSubDetail["MODEL"][$iSub];
  		  $_POST["cvtEnt"][$iSub]          = $lstSubDetail["CVT"][$iSub];
        $_POST["cvtQtyEnt"][$iSub]       = $lstSubDetail["CVT_QTY"][$iSub];
  		  $_POST["unitEnt"][$iSub]         = $lstSubDetail["UNIT"][$iSub];
  		  $_POST["qtyEnt"][$iSub]          = $lstSubDetail["QTY"][$iSub];
  		  $_POST["priceEnt"][$iSub]        = $lstSubDetail["PRICE"][$iSub];
  		  $_POST["hrzTotEnt"][$iSub]       = $lstSubDetail["HRZ_TOT"][$iSub];
  		  $_POST["discPctgEnt"][$iSub]     = $lstSubDetail["DISC_PCTG"][$iSub];
  		  $_POST["discEnt"][$iSub]         = $lstSubDetail["DISC"][$iSub];
  		  $_POST["netEnt"][$iSub]          = $lstSubDetail["NET"][$iSub];
		  }

  		/**
  		* Check if this tx isn't canceled
  		* YES: it is modify
  		* NO : Add new entry with this copy
  		*
  		*/
  		if($lstHeader["STATUS"][0] != _CANCELED){

  		  $_SESSION["modify_mode"]= "Y";
  		}else{

  		  $_SESSION["modify_mode"]= "N";
  		}

  		/**
       * transfer process
       * real --> tmp
       */
  		$_SESSION["tmpJid"] = $GProcessors->getTmpJid();

  		$CLHandlers->transDpRealToTmp($COracle,
                                    $jobId,
                                    $_SESSION["tmpJid"],
                                    $SourceDb);

  		/**
  		* This is only to by pass
  		* the entry_detail precheck
  		*
  		*/
  		//$_POST["entry"]         = "byPass";
  		$_SESSION["init_num"]   = $nSubDetail;

  		$COracle->commit();

  		$COracle->dc();

  		require("entry_restore_me.php");

  	}else{

  	  usrDie("Tx can't be modified, it has been approved");
  	}
	}else{

	  usrDie("Error: Oracle connection failed");
	}
}else{

	usrDie("Error:Cant retrieved job_id");
}

?>